from django.shortcuts import get_object_or_404, render
from django.utils import translation
from django.contrib.auth import authenticate, login as auth_login,logout
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.utils.translation import ugettext as _


def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/')
    
    return render(request, 'login.html')

def process_login(request):
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                auth_login(request, user)
                return HttpResponseRedirect('/')
            else:
                message = _("luca_auth_account_not_active")
        else:
            message = _("luca_auth_account_wrong")
        
        messages.add_message(request, messages.INFO, message)    
        return HttpResponseRedirect('/auth/login/')  
    
  
def process_logout(request):
    logout(request)
    return HttpResponseRedirect('/auth/login/')